package graficar;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class GraficoConmutadorInterno extends GraficoConmutadores{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	final double VARIACION_X=0.035;
	final double VARIACION_Y=0.035;
	
	ArrayList<GraficoTerminalInterna> ti = new ArrayList<GraficoTerminalInterna>();

	public GraficoConmutadorInterno(){

	}

	public GraficoConmutadorInterno(String id){
		this.id=id;
	}
	
	public GraficoConmutadorInterno(double x, double y, double tamX, double tamY){
		this.x=x;
		this.y=y;
		this.tamX=tamX;
		this.tamY=tamY;
	}



	public ArrayList<GraficoTerminalInterna> getTi() {
		return ti;
	}

	public void setTi(ArrayList<GraficoTerminalInterna> ti) {
		this.ti = ti;
	}


	public void setConInt(Diagrama lienzo , Ellipse2D eliPrincipal, ArrayList<GraficoConmutadorInterno> auxList, GraficoConmutadorCentral cc){

		int ancho = lienzo.getWidth();
		int alto = lienzo.getHeight();
		
		for (int i=0; i<auxList.size();i++){

			double minX,minY;

			minX=Math.random()*(eliPrincipal.getMaxX() - eliPrincipal.getX() + 1) + eliPrincipal.getX();
			minY=Math.random()*((.5*eliPrincipal.getMaxY()) - eliPrincipal.getY() +1) + eliPrincipal.getY();

			if(eliPrincipal.contains(new Rectangle2D.Double(minX,minY,(VARIACION_X*ancho),(VARIACION_Y*alto))) &&
					noSeIntersecta(new GraficoConmutadorInterno(minX, minY, (VARIACION_X*ancho), (VARIACION_Y*alto)), auxList) &&
					!(new Rectangle2D.Double(minX,minY,(VARIACION_X*ancho),(VARIACION_Y*alto)).intersects(new Rectangle2D.Double(cc.x,cc.y,cc.tamX,cc.tamY)))){
				auxList.get(i).x=minX; //posicion x
				auxList.get(i).y=minY; //posicion y
				auxList.get(i).tamX=(VARIACION_X*ancho); //ancho
				auxList.get(i).tamY=(VARIACION_Y*alto); //alto
			}
			else{
				i-=1;
			}
		}


	}

	public Boolean noSeIntersecta(GraficoConmutadorInterno ci, ArrayList<GraficoConmutadorInterno> listCi){
		for(int i=0; i<listCi.size();i++){
			if(new Rectangle2D.Double(ci.x,ci.y,ci.tamX,ci.tamY).intersects(new Rectangle2D.Double(listCi.get(i).x,listCi.get(i).y,listCi.get(i).tamX,listCi.get(i).tamY)))
				return false;
		}
		
		return true;
	}
	
	

	public void paint (Graphics g, ArrayList<GraficoConmutadorInterno> auxList){

		Graphics2D g2d = (Graphics2D) g;

		for(int i=0; i<auxList.size();i++){

			Rectangle2D recAux = new Rectangle2D.Double(auxList.get(i).x,auxList.get(i).y,auxList.get(i).tamX,auxList.get(i).tamY);

			g2d.setColor(Color.BLACK);
			g2d.fill(recAux);
			g2d.setStroke(new BasicStroke(5));
			g2d.setColor(Color.LIGHT_GRAY);
			g2d.draw(recAux);
			g2d.setColor(Color.RED);
			g2d.drawString(auxList.get(i).id, auxList.get(i).getX(), auxList.get(i).getY());
		}



	}

}
